package cn.wen.yaling.controller;

import java.util.*;
import java.util.Map;

import cn.wen.yaling.commons.enums.HttpCode;
import cn.wen.yaling.commons.service.RedisService;
import cn.wen.yaling.commons.utils.RedisUtils;
import cn.wen.yaling.commons.vo.ResourceAddVO;
import cn.wen.yaling.commons.vo.ResourceListVO;
import cn.wen.yaling.commons.vo.ResourceUpdateVO;
import cn.wen.yaling.commons.vo.ResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.ResourcesEntity;
import cn.wen.yaling.xo.service.ResourcesService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;

/**
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class ResourcesController {

    @Autowired
    private ResourcesService resourcesService;

    @GetMapping("/web/resources/list")
    public ResultEntity selectAllResources() {
        List<ResourceVO> resources = resourcesService.getAllResources();
        return ResultEntity.ok().put("data", resources);
    }

    @PostMapping("/web/resource/addition")
    public ResultEntity addResource(@RequestBody ResourceAddVO resourceAddVO) {
        // 直接调用业务层的方法实现插入
        return resourcesService.addResource(resourceAddVO);
    }

    /*=================================== 后台 Api =================================*/

    @GetMapping("/admin/resources/sort/list")
    public ResultEntity getSortList() {
        return resourcesService.getAllSortList();
    }

    @GetMapping("/admin/resource/list")
    public ResultEntity selectResourceList() {
        List<ResourceListVO> resources = resourcesService.getResourceList();
        return ResultEntity.ok().put("data", resources);
    }

    @GetMapping("/admin/resource/search")
    public ResultEntity searchResource(@RequestParam("resourceName") String resourceName) {
        List<ResourceListVO> resources = resourcesService.searchResource(resourceName);
        return ResultEntity.ok().put("data", resources);
    }

    @PostMapping("/admin/resource/add")
    public ResultEntity addAdminResource(@RequestBody ResourceAddVO resourceAddVO) {
        return resourcesService.addResource(resourceAddVO);
    }

    @PostMapping("/admin/resource/update")
    public ResultEntity updateResource(@RequestBody ResourceUpdateVO resourceUpdateVO) {
        return resourcesService.updateResource(resourceUpdateVO);
    }

    @PostMapping("/admin/resource/delete")
    public ResultEntity deleteResource(@RequestParam("resourceIds") Integer[] resourceIds) {
        return resourcesService.deleteByIds(resourceIds);
    }

    /**
     * 修改状态
     * @param resourceId
     * @return
     */
    @PostMapping("/admin/resource/update-status")
    public ResultEntity updateResourceStatus(@RequestParam("resourceId") Integer resourceId,
                                       @RequestParam("status") Integer status) {
        return resourcesService.updateResourceStatus(resourceId, status);
    }
}
